﻿using CommunityToolkit.Mvvm.ComponentModel;
using HslCommunication.Profinet.Siemens;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test
{
    class Program
    {

        private const string _OP620_IP = "192.168.10.10";
        private const string _OP640_IP = "192.168.10.25";
        private const string _OP550_IP = "192.168.10.40";
        private const string _OP560_IP = "192.168.10.55";
        private const string _OP570_IP = "192.168.10.75";
        private const string _OP580_1_IP = "192.168.10.90";
        private const string _OP580_2_IP = "192.168.10.105";
        private const string _OP610_IP = "192.168.10.120";
        static void Main(string[] args)
        {

            //PLCConnect pLCConnect = new PLCConnect();
            //pLCConnect.ConnectPLC(_OP640_IP);

            //var rs = pLCConnect.WriteData("DB1001.10", 0);

            var rs = GetPartSerialNo(1);
            Console.WriteLine(rs);

            Console.ReadLine();
        }


        /// <summary>
        /// 生产批次
        /// </summary>
        /// <returns></returns>
        private static string GetProductBatch()
        {
            var year = DateTime.Now.Year.ToString().Substring(2);
            var mouth = DateTime.Now.Month.ToString("X");
            var day = DateTime.Now.Date.ToString("dd");
            return $"{year}{mouth}{day}";
        }

        /// <summary>
        /// 生产批次
        /// </summary>
        /// <returns></returns>
        private static string GetPartSerialNo(int index)
        {
            var temp = GetProductBatch();
            var rlt = index.ToString("50000");
            return $"{temp}{rlt}";
        }

    }



    class PLCConnect : ObservableObject
    {
        private SiemensS7Net _client;
        private const int _Port = 102;
       
        public void ConnectPLC(string ip)
        {

            if (_client == null)
            {
                _client = new SiemensS7Net(SiemensPLCS.S1200);
                _client.IpAddress = ip;
                _client.SetPersistentConnection();
                _client.Port = _Port;
                PropertyChanged += SignalAction;
            }

        }


        public void SignalAction(object sender, PropertyChangedEventArgs e)
        {
            try
            {
                var signalName = e.PropertyName;

            }
            catch (Exception ex)
            {

            }
        }

        public bool WriteData(string addr, short value)
        {
            if (string.IsNullOrEmpty(addr))
            {
                return false;
            }
            if (_client != null)
            {
                Console.WriteLine($"WriteData(),ip：{_client.IpAddress}:{_client.Port} addr：{addr} value：{value}");
                return _client.Write(addr, value).IsSuccess;
            }
            return false;
        }

    }
}
